package gui;

import java.util.Observable;
import java.util.Observer;
import modelcontroller.ChatController;
import modelcontroller.ChatModel;
import modelcontroller.ChatModel.Val;
import modelcontroller.Conversation;

/**
 *
 * @author Mariam
 */
public class ChatGUI implements Observer {

    protected ChatModel model;
    protected ChatController controller;
    private ConnectionWindow connectionWindow;
    private ChatWindow chatWindow;

    public ChatGUI(ChatModel model) {
        this.model = model;
        initView();
    }

    public void setController(ChatController controller) {
        this.controller = controller;
    }

    public void connect(String username) {
        controller.performConnect(username);
    }

    public void initView() {
        connectionWindow = new ConnectionWindow(this);
    }

    public void changeView() {
        if (model.getLocalUserIsConnected()) {
            this.connectionWindow.setVisible(false);
            this.chatWindow = new ChatWindow(this);
        } else {
            this.chatWindow.setVisible(false);
            //this.connectionWindow.setVisible(true);
            //this.chatWindow.dispose();
            //this.connectionWindow.dispose();
        }
    }

    public void close() {
        System.exit(0);
    }

    @Override
    public void update(Observable o, Object arg) {
        System.out.println("UPDATE");
        if (arg.equals(Val.STATE)) {
            changeView();
        } else if (arg.equals(Val.USERSLIST)) {
            chatWindow.updateUsersList();
        } else if (arg.equals(Val.CONVERSATIONSLIST)) {
            chatWindow.updateConversationsList();
        }
    }

    public void addTab(Conversation c) {
        chatWindow.openConversation(c);
        chatWindow.updateConversationsList();
    }

    public boolean notifyFileRequest(String username, String filename) {
        return chatWindow.answerFileRequest(username, filename);
    }

    public String askFilepath() {
        return chatWindow.answerFilepath();
    }
}
